<?php

function objectToArray(&$object) {
    $object = (array) $object;
    foreach ($object as $key => $value) {
        if (is_object($value)) {
            objectToArray($value);
            $object[$key] = $value;
        }
        if (is_array($value)) {
            objectToArray($value);
            $object[$key] = $value;
        }
    }
}

function chagearray($sellarray) {
    if (isset($sellarray[1])) {//判断是否为一个分类的一维数组
        foreach ($sellarray as $key => $val) {
            $karray[$val["cid"]] = array("name" => $val["name"], "parent_cid" => $val["parent_cid"]);
        }
        foreach ($karray as $key => $val) {
            foreach ($karray as $k => $v) {
                if ($key == $v["parent_cid"]) {
                    $karray[$key][$k] = $v["name"];
                    unset($karray[$k]);
                }
            }
        }
    } else {
        $karray = array($sellarray["cid"] => array("name" => $sellarray["name"]));
    }
    return $karray;
}

function chagearray2($sellarray) {

    $karray = chagearray($sellarray);

    foreach ($karray as $key => $val) {
        foreach ($val as $k => $v) {
            if (($k != "name") && ($k != "parent_cid")) {
                $karray[$key]["sub"][$k] = $v;
                unset($karray[$key][$k]);
            }
        }
    }
    foreach ($karray as $key => $val) {
        unset($karray[$key]["parent_cid"]);
    }
    return $karray;
}

function generatewanwagarray($shopid) {
    $CI = & get_instance();
    $CI->load->database();
    $sql = "select * from groups where shopid=\"{$shopid}\"";
    $res = $CI->db->query($sql);
    $resarray = $res->result_array();

    foreach ($resarray as $key => $val) {
        $wang_array[$key]['name'] = $val["groupname"];
        $sql = "
        select childaccount.nicekname
 from client,childaccount 
 where client .shopid=\"{$shopid}\" and  client .groupid=\"{$val["roleid"]}\" and client.subid=childaccount.subid 
 and client.shopid=childaccount.shopid
 ";   //      select * from client where shopid=\"{$shopid}\"and groupid={$val["id"]}  
        $res = $CI->db->query($sql);
        $resarray = $res->result_array();

        foreach ($resarray as $k => $v) {
            $wang_array[$key]['group'][$k] = $v["nicekname"];
        }
    }
    return $wang_array;
}

function get_all_child($shopid) {
    $CI = & get_instance();
    $CI->load->database();
    $sql = "select * from childaccount where shopid=\"{$shopid}\"";
    $res = $CI->db->query($sql);
    $resarray = $res->result_array();
    return $resarray;
}

function getshopid($nickname) {//登录的时候获的所属的shopid
    $CI = & get_instance();
    $CI->load->library('topclass');

    $resp = $CI->topclass->api_getShopGetRequest("ShopGetRequest", $nickname);
    return $shopid = (string) $resp->shop->sid;
}

function getserverarray($nickname, $sessionley) {//通过topapi获得子账户分组
    $CI = & get_instance();
    $CI->load->library('topclass');
    $resp = $CI->topclass->api_SellercenterSubusersGetRequest("SellercenterSubusersGetRequest", $nickname, $sessionley);
    objectToArray(&$resp);
    return $resp;
}

function get_tmp_width($Modulename) {//获得模板的宽度
    $result = explode("/", $Modulename);
    $result = explode("_", $result[1]);
    return $result[0];
}

function get_shop_tmpcounts($shopid) {

    $CI = & get_instance();
    $CI->load->database();
    $CI->config->load('infoconfig');
    $width_configarray = $CI->config->item('tmpwidth');
    foreach ($width_configarray as $key => $val) {

        $sql = "select count(id) as counts from res where shopid={$shopid} and width=\"{$val}\" ";
        $res = $CI->db->query($sql);
        $resarray = $res->result_array();

        $returnarray[$val] = $resarray[0]["counts"];
    }
    return $returnarray;
}

function get_tmpres($shopid) {//获得店铺模板的数组
    $CI = & get_instance();
    $CI->load->database();
    $CI->config->load('infoconfig');
    $width_configarray = $CI->config->item('tmpwidth');

    foreach ($width_configarray as $key => $val) {

        $sql = "select * from res where shopid={$shopid} and width=\"{$val}\" ";
        $res = $CI->db->query($sql);
        $resarray = $res->result_array();
        foreach ($resarray as $k => $v) {
            $returnarray[$val][$v["id"]]["name"] = $v["tmpname"];
            $returnarray[$val][$v["id"]]["staute"] = $v["staute"];
            $returnarray[$val][$v["id"]]["time"] = $v["time"];
            $returnarray[$val][$v["id"]]["htmlcontent"] = $v["content"];
            $returnarray[$val][$v["id"]]["tmpath"] = $v["tmpath"];
        }
    }
    if (!isset($returnarray)) {
        return;
    } else {

        return ($returnarray);
    }
}

function decis_last_time() {
    $_sess_time = 14400;
    if (!isset($_SESSION['last_active_time'])) {
        redirect('login', 'location', 301);
    }
    if (($_SESSION['last_active_time'] + $_sess_time) < time()) {
        //$_msg[1] = '错误提示: 登录已经超时.[ERROR02]';
        redirect('login/logout', 'location', 301);
    } else {
        $_SESSION['last_active_time'] = time();
    }
}

function f100colors() {
    return array('#FF0176', '#32305F', '#9A22DE', '#77BB43', '#D0752A', '#FF0176',
        '#FFC2BF', '#A4DBDE', '#8FCBA9', '#D9E96D', '#D4AFD1', '#FF65A3', '#49C8CF', '#A1D125', '#FD4C56',
        '#FBBB11', '#D6BA7B', '#8798A8', '#2A2C41', '#613D59', '#9970BE', '#16499A', '#0CA4C9', '#670001',
        '#613D59', '#FF7F00', '#015F47', '#F02020', '#48AC16', '#477E93', '#96B7FC', '#C0C0C0',
        '#7C79F9', '#A45ECD', '#FC6D27', '#F89327', '#7FBA01', '#4C9BEB', '#317EE7', '#998673', '#637ABD', '#D99898', '#7BB1B1',
        '#9F88AD', '#8D7F7E', '#229B8E', '#3C3C3C', '#861004', '#70C5FF', '#D5B18D', '#E4EAA0');
}

function get_segment_fuc($seg_num) {
    $CI = & get_instance();
    if ($CI->uri->segment($seg_num) === FALSE) {
        return;
    } else {
        $id = $CI->uri->segment($seg_num);
        if (is_numeric($id)) {
            return $id;
        } else {
            return;
        }
    }
}


 function curl($url, $postFields = null)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_FAILONERROR, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		if (is_array($postFields) && 0 < count($postFields))
		{
			$postBodyString = "";
			foreach ($postFields as $k => $v)
			{
				$postBodyString .= "$k=" . urlencode($v) . "&"; 
			}
			unset($k, $v);
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);  
 			curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
		}
		$reponse = curl_exec($ch);
                
               
		if (curl_errno($ch)){
			throw new Exception(curl_error($ch),0);
		}
		else{
			 $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			if (200 !== $httpStatusCode){
				throw new Exception($reponse,$httpStatusCode);
			}
		}
		curl_close($ch);
		return $reponse;
	}
 
/*
function get_url_host($url) {//获取url主地址
    $url_array = parse_url($url);
    return $url_array["host"];
}

function get_urlquery_seg($url) {//获取url的query地址
    $url_array = parse_url($url);
    return $url_array["query"];
}

function get_urlpath_seg($url) {//获取url的path地址
    $url_array = parse_url($url);
    return $url_array["path"];
}

function get_url_itemid($url_query) {//获取url的itmeid地址
    parse_str($url_query, $myArray);
    print_r($myArray);
    echo $myArray["id"];
    // $url_query_array = explode("&", $url_query);
    // print_r($url_query_array);
}

function get_url_root($url) {//判断url是否属于淘宝系
    $hosturl = get_url_host($url);
    $CI = & get_instance();
    $CI->config->load('sysetting', TRUE);
    $characters_array = $CI->config->item('host_url'); //获取配置数组
    $istaobao_url = stristr($hosturl, $characters_array["taobao_charact"]);
    $istianmao_url = stristr($hosturl, $characters_array["tianmao_charact"]);
    if ($istaobao_url || $istianmao_url) {
        echo "符合淘宝系列网址命名规则";
        echo $path = get_urlpath_seg($url);
        if ($path == "/item.htm") {
            $query = get_urlquery_seg($url);
            get_url_itemid($query);
        }else{            
            echo "应该为淘宝相关活动页面，读取数据库中店铺类型字段生成淘宝店铺
                url";
        }
    } else {
        echo "非淘宝系列网址";
    }
}
*/
